package com.ita.os.merchant.web.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ita.os.merchant.po.Merchant;
import com.ita.os.merchant.service.MerchantService;
import com.ita.os.merchant.service.impl.MerchantServiceImpl;

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private MerchantService merchantService = new MerchantServiceImpl();
    public LoginServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String tel = request.getParameter("tel");
        String password = request.getParameter("password");
		Merchant merchant = merchantService.findMerchant(tel, password);
		if(merchant==null){
            String message = String.format(
                    "对不起，用户名或密码有误！！请重新登录！2秒后为您自动跳到登录页面！！<meta http-equiv='refresh' content='2;url=%s'", 
                    request.getContextPath()+"/loginUi");
            request.setAttribute("message",message);
            request.getRequestDispatcher("/message.jsp").forward(request, response);
            return;
        }
        request.getSession().setAttribute("merchant", merchant);
        String message = String.format(
                "恭喜：%s,登陆成功！本页将在2秒后跳到首页！！<meta http-equiv='refresh' content='2;url=%s'", 
                merchant.getMname(),"index");
        request.setAttribute("message",message);
        request.getRequestDispatcher("/message.jsp").forward(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
